package com.yanqu.road.server.cmd.activity.hanghai.cross;

import com.yanqu.road.entity.activity.hanghai.config.HangHaiConfig;
import com.yanqu.road.entity.activity.seasontask.SeasonTaskConditionData;
import com.yanqu.road.entity.enums.eActivityType;
import com.yanqu.road.entity.enums.eGamePlayerEventType;
import com.yanqu.road.entity.enums.eLogMoneyType;
import com.yanqu.road.pb.activity.HangHaiActivityProto;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.player.CurrencyModule;
import com.yanqu.road.server.gameplayer.module.player.PreReduceModule;
import com.yanqu.road.server.manger.activity.hanghai.HangHaiMgr;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.ClientProtocol;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.GameProtocol;
import com.yanqu.road.utils.property.PropertyHelper;

import java.math.BigInteger;

@Cmd(code = GameProtocol.S_HANGHAI_ACTIVITY_SPEED_UP_FROM_CROSS, desc = "（跨服返回）返回加速")
public class HangHaiSpeedUpFromCrossCmd extends AbstractCommand {

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        return true;
    }

    @Override
    public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
        HangHaiConfig config = HangHaiMgr.getHangHaiConfig();
        if (config == null || !HangHaiMgr.isInOperationTime(config)) {
            ret = GameErrorCode.E_HANGHAI_ACTIVITY_NO_OPEN;
            return;
        }

        HangHaiActivityProto.CrossHangHaiSpeedUpRespMsg respMsg = HangHaiActivityProto.CrossHangHaiSpeedUpRespMsg.parseFrom(packet.getBytes());
        HangHaiActivityProto.HangHaiSpeedUpRespMsg resp = respMsg.getResp();
        int realTimes = respMsg.getRealTimes();
        int times = respMsg.getTimes();

        // 预扣除返还
        player.getModule(PreReduceModule.class).restorePreReduce(PropertyHelper.parseGoodsToString(config.getSpeedUpItemId(), BigInteger.valueOf(times)));

        // 扣道具
        if (resp.getRet() == 0) {
            player.getModule(CurrencyModule.class).removeCurrency(
                    PropertyHelper.parseStringToProperty(config.getSpeedUpItemId() + "=" + realTimes),
                    eLogMoneyType.HangHaiActivity,
                    eLogMoneyType.HangHaiSpeedUpCost
            );
        }
        SeasonTaskConditionData data = new SeasonTaskConditionData(BigInteger.valueOf((long) HangHaiMgr.getHangHaiConfig().getActivityInfo().getType()), BigInteger.valueOf((long)realTimes));
        player.notifyListener(eGamePlayerEventType.HangCostStaminaTask.getValue(), data);

        // 返回
        player.sendPacket(ClientProtocol.U_HANGHAI_ACTIVITY_SPEED_UP, resp.toBuilder());
    }

    @Override
    public void sendData(GamePlayer player) {
        if (ret != 0) {
            HangHaiActivityProto.HangHaiSpeedUpRespMsg.Builder builder = HangHaiActivityProto.HangHaiSpeedUpRespMsg.newBuilder();
            builder.setRet(ret);
            player.sendPacket(ClientProtocol.U_HANGHAI_ACTIVITY_SPEED_UP, builder);
        }
    }
}
